﻿using CPPEI.Coolzon.UI.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CPPEI.Coolzon.Theme.Fashion.MainViewControl
{
    /// <summary>
    /// Interaction logic for ExtentionFunctionPanel.xaml
    /// </summary>
    public partial class ExtentionFunctionPanel : Window
    {
        //public event Action Cancel;

        public ExtentionFunctionPanel()
        {
            InitializeComponent();


            // 读取扩展方法定义
            //List<ExtentionFunction> functions = ExtentionFunctionLoader.Instance.Load(string.Format("{0}/config/extentionfunction.xml", AppDomain.CurrentDomain.BaseDirectory));

            //for (int index = 0; index < functions.Count; index++)
            //{
            //    Button btn = new Button();
            //    btn.Width = 90;
            //    btn.Height = 40;
            //    btn.Margin = new Thickness(5);
            //    btn.Tag = functions[index];
            //    btn.Click += btn_Click;
            //    TextBlock contentText = new TextBlock();
            //    contentText.Text = functions[index].Caption;
            //    contentText.TextWrapping = TextWrapping.Wrap;
            //    contentText.HorizontalAlignment = System.Windows.HorizontalAlignment.Center;
            //    contentText.VerticalAlignment = System.Windows.VerticalAlignment.Center;
            //    btn.Content = contentText;
            //    this._ctlPanel.Children.Add(btn);
            //}
        }

        void btn_Click(object sender, RoutedEventArgs e)
        {
            ExtentionFunction function = (sender as Button).Tag as ExtentionFunction;
            if (string.Equals(function.Mode, "ExternalProgram", StringComparison.CurrentCultureIgnoreCase))
            {
                // 调用外部程序
                try
                {
                    System.Diagnostics.ProcessStartInfo info = new System.Diagnostics.ProcessStartInfo();
                    info.FileName = function.Action;
                    info.Arguments = function.Arguments;
                    info.Verb = "runas";
                    System.Diagnostics.Process.Start(info);
                    Coolzon.Common.Logger.DebugToTag("UI", "run ExternalProgram {0} {1}", function.Arguments, function.Arguments);
                }
                catch(Exception ex)
                {
                    Coolzon.Common.Logger.ErrorToTag("UI", "run ExternalProgram {0} {1}, ex:{2}", 
                        function.Arguments,
                        function.Arguments,
                        ex);
                }
            }
        }

        private void _ctlBtnCancel_Click_1(object sender, RoutedEventArgs e)
        {
            Close();
            //if (Cancel != null)
            //{
            //    Cancel();
            //}
        }
    }
}
